<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/6/17
 * Time: 17:03
 */

namespace Home\Controller;
use Think\Controller;
class StatisticController extends Controller
{
    /**
     * 总量统计
     * 每日 00:05 执行，统计前一天的设备 安装【1】 卸载【2】 日活【3】 当日安装后又卸载的 添加到原来统计数据的数据
     */
    public function StatisticDays(){
        $data = array('install' => 0,'uninstall' => 0,'active' => 0,'installun' => 0);
        $date = date("Ymd",strtotime('-1 days'));
        $table_name = 'epduba_lieying_install_'.$date;
        //创建记录总量的文件
        $context = file_get_contents('StatisticDateData.json');
        if($context == ""){ // 文件为空表示首次写入
            $in_txt = json_encode($data);
            $handle =fopen('StatisticDateData.json','w+');
            if($handle){
                fwrite($handle,$in_txt);
                fclose($handle);
            }
        }
        $result = D('Install') -> getDateData($table_name);
        foreach($result as $today_data){
            switch($today_data['active']){
                case '1':
                    $today_data_tmp['install'] = $today_data['u_count'];
                    break;
                case '2':
                    $today_data_tmp['uninstall'] = $today_data['u_count'];
                    break;
                case '3':
                    $today_data_tmp['active'] = $today_data['u_count'];
                    break;
            }
        }
        //今日安装后又卸载的数据
        $ret = D('Install') -> getInstallUnAllData($table_name);
        $today_data_tmp['installun'] = $ret;
        // 文件中是否有之前的统计数据
        $old_context = file_get_contents('StatisticDateData.json');
        $data = json_decode($old_context,TRUE);
        $new_install = intval($data['install']) + intval($today_data_tmp['install']);
        $new_uninstall =intval($data['uninstall']) + intval($today_data_tmp['uninstall']);
        $new_active =intval($data['active']) + intval($today_data_tmp['active']);
        $new_installun =intval($data['installun']) + intval($today_data_tmp['installun']);
        $new_data = array(
            'install' => $new_install,
            'uninstall' => $new_uninstall,
            'active' => $new_active,
            'installun' => $new_installun
        );
        $new_in_txt = json_encode($new_data);
        $handle = fopen('StatisticDateData.json','w+');
        fwrite($handle,$new_in_txt);
        fclose($handle);
    }
    //从统计表中读取当日数据 累计到 json文件中
    public function StatisticDaysFromNewTable(){
        $data = array('install' => 0,'uninstall' => 0,'active' => 0,'installun' => 0);

        //创建记录总量的文件
        $context = file_get_contents('StatisticDateData.json');
        if($context == ""){ // 文件为空表示首次写入
            $in_txt = json_encode($data);
            $handle =fopen('StatisticDateData.json','w+');
            if($handle){
                fwrite($handle,$in_txt);
                fclose($handle);
            }
        }
        //从统计表中读取数据
        $b_date = $e_date = date("Y-m-d",strtotime('-1 days'));
        $result = D('Statistic') -> getCountsDur($b_date, $e_date);
        foreach($result as $today_data){
            $today_data_tmp['install'] = $today_data['install'];
            $today_data_tmp['uninstall'] = $today_data['uninstall'];
            $today_data_tmp['active'] = $today_data['active'];
        }

        //今日安装后又卸载的数据
        $date = date("Ymd",strtotime('-1 days'));
        $table_name = 'epduba_lieying_install_'.$date;
        $ret = D('Install') -> getInstallUnAllData($table_name);
        $today_data_tmp['installun'] = $ret;

        // 文件中是否有之前的统计数据
        $old_context = file_get_contents('StatisticDateData.json');
        $data = json_decode($old_context,TRUE);
        $new_install = intval($data['install']) + intval($today_data_tmp['install']);
        $new_uninstall =intval($data['uninstall']) + intval($today_data_tmp['uninstall']);
        $new_active =intval($data['active']) + intval($today_data_tmp['active']);
        $new_installun =intval($data['installun']) + intval($today_data_tmp['installun']);
        $new_data = array(
            'install' => $new_install,
            'uninstall' => $new_uninstall,
            'active' => $new_active,
            'installun' => $new_installun
        );
        $new_in_txt = json_encode($new_data);
        $handle = fopen('StatisticDateData.json','w+');
        fwrite($handle,$new_in_txt);
        fclose($handle);
    }
    //每日数据统计
    /**
     * 统计 安装、卸载、日活、当日安装即卸载
     */
    public function StatisticDay(){
        $table_name = $this -> CreateTb('day');
        $date = date("Ymd",strtotime('-1 days'));
        $log_tb_name = "epduba_lieying_install_".$date;
        D('Statistic') -> dayDataStatistic($log_tb_name);
    }

    /**
     * 创建按日统计表
     */
    private function CreateTb($type){
        if($type == 'day')
            $tb = D('Statistic') -> createStatisticTable();
        else
            $tb = D('Statistic') -> createChannelTable();
        return $tb;
    }
    /**
     * 按渠道 | 日期 统计 安装、卸载、日活、当日安装即卸载
     */
    public function StatisticChannelDay(){
        $table_name = $this -> CreateTb('channel');
        $date = date("Ymd",strtotime('-1 days'));
        $log_tb_name = "epduba_lieying_install_".$date;
        D('Statistic') -> dayChannelDataStatic($log_tb_name);
    }
}